package com.sunni.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sunni.entity.PageListRes;
import com.sunni.entity.QueryVo;
import com.sunni.entity.Role;
import com.sunni.service.RoleService;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /*tree.json访问role.jsp*/
    @RequestMapping("/getJspPage")
//    @RequiresPermissions("employee:delete")
    public String getJspPage(){
        return "role";
    }

    @RequestMapping("/selectAllRoles")
    @ResponseBody
    public PageListRes selectAllRoles(QueryVo queryVo){
        return roleService.selectAllRoles(queryVo);
    }

    @RequestMapping("/addRole")
    @ResponseBody
    public AjaxRes addRole(Role role){
        AjaxRes ajaxRes = new AjaxRes(false,"保存失败");

        try {
            roleService.addRoleAndRolePermissionRel(role);
            ajaxRes.setSuccess(true);
            ajaxRes.setMsg("保存成功");
        } catch (Exception e) {
            System.out.println(e);
        }
        return ajaxRes;
    }

    /*角色管理，编辑按钮，修改角色*/
    @RequestMapping("/updateRoleAndRolePermissionRel")
    @ResponseBody
    public AjaxRes updateRoleAndRolePermissionRel(Role role){
        AjaxRes ajaxRes = new AjaxRes(false,"修改失败");

        try {
            roleService.updateRoleAndRolePermissionRel(role);
            ajaxRes.setSuccess(true);
            ajaxRes.setMsg("修改成功");
        }catch (Exception e){
            System.out.println(e);
        }

        return ajaxRes;
    }

    /*角色管理，编辑按钮，删除角色*/
    @RequestMapping("/deleteRoleAndRolePermissionRel")
    @ResponseBody
    public AjaxRes deleteRoleAndRolePermissionRel(Long rid){
        AjaxRes ajaxRes = new AjaxRes(false,"删除失败");


        try {
            roleService.deleteRoleAndRolePermissionRel(rid);
            ajaxRes.setSuccess(true);
            ajaxRes.setMsg("删除成功");
        } catch (Exception e) {
            System.out.println("错误信息------------ = " + e);
        }
        return ajaxRes;
    }

    /*查询所有角色role表，来自employee dialog 下拉框*/
    @RequestMapping("/selectAllRole")
    @ResponseBody
    public List<Role> selectAllRole(){
        return roleService.selectAllRoles();
    }


    /*处理 没有对应的操作的授权 报的500错误  告诉页面没有权限*/
    @ExceptionHandler(AuthorizationException.class ) /*出现AuthorizationException异常，就执行这个方法*/
    public void handleShiroException(HandlerMethod method , HttpServletResponse response) throws Exception{

        /*获取报错那个方法的注解，看看有没有ResponseBody这个注解*/
        ResponseBody responseBody = method.getMethodAnnotation(ResponseBody.class);

        if (responseBody != null){ /*有ResponseBody注解，说明前台发的是Ajax请求*/
            AjaxRes ajaxRes = new AjaxRes(false,"没有权限 ");
            String result = new ObjectMapper().writeValueAsString(ajaxRes);
            response.setCharacterEncoding("utf-8");
            response.getWriter().write(result);
        }else {/*没有ResponseBody注解，说明前台发的不是Ajax请求*/
            response.sendRedirect("/employee/getNoPermissionJsp");
        }
    }
    @RequestMapping("/getNoPermissionJsp")
    public String getNoPermissionJsp(){
        return "nopermission";
    }

}
